﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class PhieuXuatKho : Form
    {
        
        Business.PhieuXuatKho phieuXuatKho;
        

       
        public PhieuXuatKho()
        {
            InitializeComponent();
        }


        public void LayTheoHDB(int maHDB)
        {
            phieuXuatKho = Business.PhieuXuatKho.TaoPhieuXuatKho(maHDB);
        }


        public void LayTheoPXK(int maPXK)
        {
            phieuXuatKho = Business.PhieuXuatKho.LayPhieuXuatKho(maPXK);
        }

        private void PhieuXuatKho_Load(object sender, EventArgs e)
        {
            LoadThongTinPhieuXuatKho();
            if (@Session.quyen == "1" || @Session.quyen == "2") 
            {
                btnLapPhieuGH.Enabled = false;
                return;
            }
            if (@Session.quyen == "3") 
            {
                btnLuu.Enabled = false;
                return;
            }

        }

        private void LoadThongTinPhieuXuatKho()
        {
            if (phieuXuatKho.MaPXK != 0)
                txtMaPhieuXuatKho.Text = phieuXuatKho.MaPXKHienThi;
            txtMaHoaDonBan.Text = phieuXuatKho.MaHDBHienThi;

            txtNV.Text = "NV" + @Session.maNV;

            txtLiDoXuat2.Text = phieuXuatKho.LyDoXuat;

            txtKhachHang.Text = phieuXuatKho.TenKhachHang;
            txtDiaChi2.Text = phieuXuatKho.DiaChiKhachHang;
            txtDienThoai2.Text = phieuXuatKho.DienThoai;

            LoadDanhSachchiTietMatHang();
        }

        private void LoadDanhSachchiTietMatHang()
        {
            BindingSource source = new BindingSource();
            source.DataSource = phieuXuatKho.DanhSachChiTietMatHang;

            gridViewChiTietMatHang.DataSource = source;
            gridViewChiTietMatHang.RefreshEdit();
            gridViewChiTietMatHang.Refresh();

            gridViewChiTietMatHang.Columns["MaCTHDB"].Visible = false;
            gridViewChiTietMatHang.Columns["MaHang"].Visible = false;

            gridViewChiTietMatHang.Columns["MaHDB"].Visible = false;

            gridViewChiTietMatHang.Columns["ChietKhau"].Visible = false;

            gridViewChiTietMatHang.Columns["MaCTHDBHienThi"].HeaderText = "Mã chi tiết đặt";
            gridViewChiTietMatHang.Columns["MaHangHienThi"].HeaderText = "Mã hàng";


            gridViewChiTietMatHang.Columns["TenHang"].HeaderText = "Tên hàng";

            gridViewChiTietMatHang.Columns["SoLuong"].HeaderText = "Số lượng";
            gridViewChiTietMatHang.Columns["DonGiaBan"].HeaderText = "Đơn giá bán";
            gridViewChiTietMatHang.Columns["ChietKhauHienThi"].HeaderText = "Chiết khấu (%)";

            gridViewChiTietMatHang.Columns["DonGiaBan"].DefaultCellStyle.Format = "##,###,###";
        }
        

        
        private void CapNhatThongTinChung()
        {
            phieuXuatKho.MaNV = Int16.Parse(txtNV.Text.Substring(2));
            phieuXuatKho.NgayXuatHD = dtpickerNgayXuat.Value;

            phieuXuatKho.LyDoXuat = txtLiDoXuat2.Text;
        }


        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (phieuXuatKho.MaPXK == 0)
            {

                CapNhatThongTinChung();
                int maPXk = Business.PhieuXuatKho.ThemMoi(phieuXuatKho);

                phieuXuatKho = Business.PhieuXuatKho.LayPhieuXuatKho(maPXk);

                LoadThongTinPhieuXuatKho();
                MessageBox.Show("Thêm mới phiếu xuất kho thành công");
            }
            else
            {
                CapNhatThongTinChung();

                Business.PhieuXuatKho.CapNhat(phieuXuatKho);
                phieuXuatKho = Business.PhieuXuatKho.LayPhieuXuatKho(phieuXuatKho.MaPXK);

                LoadThongTinPhieuXuatKho();
                MessageBox.Show("Cập nhật phiếu xuất kho thành công");
            }
        }




        private void btnLapPhieuGH_Click(object sender, EventArgs e)
        {
            if (phieuXuatKho.MaPXK == 0)
            {
                MessageBox.Show("Phải lưu phiếu xuất kho trước khi lập phiếu bán hàng");
            }
            else
            {
                this.Close();
                PhieuGiaoHang form = new PhieuGiaoHang();
                form.LayTheoHDB(phieuXuatKho.MaHDB);

                form.ShowDialog();
            }
        }
     
    }
}
